
package com.jojoteamwork.photo.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;

import kankan.wheel.R;
import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.NumericWheelAdapter;

public class PasswordActivity extends Activity {
    LinearLayout plPassword;

    ImageView btnLock;

    private String password;

    boolean isUpate;

    StringBuilder pswBuilder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.passw_layout);
        SharedPreferences preferences = getSharedPreferences("setting", MODE_PRIVATE);
        password = preferences.getString("psw", null);
        if (password == null || TextUtils.isEmpty(password)) {
            isUpate = false;
            pass();
            return;
        }
        pswBuilder = new StringBuilder();
        isUpate = getIntent().getBooleanExtra("update", false);
        initWheel(R.id.passw_1);
        initWheel(R.id.passw_2);
        initWheel(R.id.passw_3);
        initWheel(R.id.passw_4);
        plPassword = (LinearLayout) findViewById(R.id.plPassword);
        btnLock = (ImageView) findViewById(R.id.btnLock);
        btnLock.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // plPassword.setAnimation()
                btnLock.setAnimation(AnimationUtils.loadAnimation(PasswordActivity.this,
                        R.anim.anim_move));
                btnLock.setVisibility(View.GONE);
                plPassword.setAnimation(AnimationUtils.loadAnimation(PasswordActivity.this,
                        R.anim.anim_show));
                plPassword.setVisibility(View.VISIBLE);
            }
        });
    }

    private void pass() {
        Intent intent;
        if (isUpate) {
            intent = new Intent(this, SetPasswordActivity.class);
        } else {
            intent = new Intent(this, MagazineActivity.class);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }

    /**
     * Initializes wheel
     * 
     * @param id the wheel widget Id
     */
    private void initWheel(int id) {
        WheelView wheel = getWheel(id);
        wheel.setViewAdapter(new NumericWheelAdapter(this, 0, 9));
        wheel.setCurrentItem((int) (Math.random() * 10));
        wheel.addChangingListener(changedListener);
        wheel.addScrollingListener(scrolledListener);
        wheel.setCyclic(true);
        wheel.setInterpolator(new AnticipateOvershootInterpolator());
    }

    /**
     * Returns wheel by Id
     * 
     * @param id the wheel Id
     * @return the wheel with passed Id
     */
    private WheelView getWheel(int id) {
        return (WheelView) findViewById(id);
    }

    // Wheel scrolled flag
    private boolean wheelScrolled = false;

    // Wheel scrolled listener
    OnWheelScrollListener scrolledListener = new OnWheelScrollListener() {
        public void onScrollingStarted(WheelView wheel) {
            wheelScrolled = true;
        }

        public void onScrollingFinished(WheelView wheel) {
            wheelScrolled = false;
            updateStatus();
        }
    };

    // Wheel changed listener
    private OnWheelChangedListener changedListener = new OnWheelChangedListener() {
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            if (!wheelScrolled) {
                updateStatus();
            }
        }

    };

    private void updateStatus() {

        pswBuilder.append(getWheel(R.id.passw_1).getCurrentItem())
                .append(getWheel(R.id.passw_2).getCurrentItem())
                .append(getWheel(R.id.passw_3).getCurrentItem())
                .append(getWheel(R.id.passw_4).getCurrentItem());

        if (pswBuilder.toString().equals(password)) {
            pass();
        } else {
            pswBuilder.delete(0, pswBuilder.length());
        }
    }

}
